package com.ray.controller;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ray.exception.ServiceException;
import com.ray.pojo.MyUser;
import com.ray.service.ShoppingMallService;
import com.ray.util.RequestUtil;
import com.ray.util.ResponseUtil;
import com.ray.util.ReturnStringUtil;

@Controller
@RequestMapping("/shoppingMall")
public class ShoppingMallController {

	@Autowired
	ShoppingMallService shoppingMallService;
	
	@RequestMapping("/{methodName}")
	@ResponseBody
	private Map<String, Object> shoppingMallController(HttpServletRequest request,HttpServletResponse response,
			@PathVariable("methodName") String methodName) throws NoSuchMethodException, SecurityException, IllegalAccessException {
		String methodNameMapping=urlMethodMappingMap.get(methodName);
		Map requestParamMap = getRequestParam(request,methodNameMapping);
		requestParamMap.put("request", request);
		requestParamMap.put("response", response);
		Method targetMethod = shoppingMallService.getClass().getMethod(methodNameMapping, Map.class);
		Object object=null;
		try {
			 object=targetMethod.invoke(shoppingMallService, requestParamMap);
		}catch(InvocationTargetException e) {
			if(e.getCause().getClass()== IllegalArgumentException.class || e.getCause().getClass() ==ServiceException.class) {
				e.printStackTrace();
				Assert.notNull(null,e.getTargetException().getMessage());
			}else {
				e.printStackTrace();
				return ResponseUtil.ResponseError(ReturnStringUtil.ERROR);
			}
		}
		return ResponseUtil.ResponseResult(object);
	}

	/**
	 * 获取登录参数
	 * @param request
	 * @return
	 */
	private Map getRequestParam(HttpServletRequest request,String methodNameMapping) {
		/**
		 * 如果方法免登陆，查看是否登录，若已登录则赋登录信息
		 */
		if(canNotLoginUrlSet.contains(methodNameMapping)) {
			MyUser myUser=(MyUser) request.getSession().getAttribute("userLogin");
			if(null==myUser) {
				return RequestUtil.getRequestAllParamNotLogin(request);
			}
		}
		return RequestUtil.getRequestAllParam(request);
	}

	/**
	 *  方法名和请求路径映射
	 */
	private static  Map<String, String> urlMethodMappingMap;
	/**
	 * 可以不登录访问的接口
	 */
	private static Set<String> canNotLoginUrlSet=new HashSet<String>();
	static{
		//请求路径
		urlMethodMappingMap=new HashMap<String,String>();
		urlMethodMappingMap.put("addShoppingCallMaterialBySelected", "addShoppingCallMaterialBySelected");//通过选择仓库物料发布产品
		urlMethodMappingMap.put("addShoppingCallNew", "addShoppingCallNew");//新增物料发布
		urlMethodMappingMap.put("getShoppingCallMaterialData", "getShoppingCallMaterialData");//获取商城已发布物料(本公司)
		urlMethodMappingMap.put("updateShoppingCallMaterial", "updateShoppingCallMaterial");//修改物料信息
		urlMethodMappingMap.put("offShoppingCallMaterial", "offShoppingCallMaterial");//下架物料
		urlMethodMappingMap.put("getAllShoppingCallMaterialData", "getAllShoppingCallMaterialData");//获取所有发布的物料，不分公司
		urlMethodMappingMap.put("getOneCompanyMaterial", "getOneCompanyMaterial");//获取某个公司发布的所有物料
		urlMethodMappingMap.put("uploadShoppingCallMaterialByExcel", "uploadShoppingCallMaterialByExcel");//使用表格上传物料(暂时还未写)
		urlMethodMappingMap.put("getAllFactoryName", "getAllFactoryName");//获取所有厂家名称
		urlMethodMappingMap.put("importShoppingMaterialToMyCompany", "importShoppingMaterialToMyCompany");//将商城物料导入到本公司  点击导入按钮的时候
		urlMethodMappingMap.put("purchaseShoppingCallMaterial", "purchaseShoppingCallMaterial");//直接采购商城物料
		urlMethodMappingMap.put("getShoppingCallImportToCompanyRecord", "getShoppingCallImportToCompanyRecord");//获取从商城导入物料记录
		urlMethodMappingMap.put("purchaseImportRecordMaterial", "purchaseImportRecordMaterial");//从物料导入记录里下采购单
		urlMethodMappingMap.put("getAllPurchaseMyCompanyMaterial", "getAllPurchaseMyCompanyMaterial");//获取所有采购我公司物料的信息
		urlMethodMappingMap.put("getShoppingCallMaterialType", "getShoppingCallMaterialType");//获取商城物料分类信息
		urlMethodMappingMap.put("setShoppingCallContactUser", "setShoppingCallContactUser");//设置商城联系人
		urlMethodMappingMap.put("getShoppingCallContactUser", "getShoppingCallContactUser");//获取商城联系人
		urlMethodMappingMap.put("getAllShoppingCallCompany", "getAllShoppingCallCompany");//获取所有商城公司
		urlMethodMappingMap.put("getShoppingCallCompanyByIds", "getShoppingCallCompanyByIds");//通过id集合获取商城物料
		urlMethodMappingMap.put("getCanImportShoppingCallMaterial", "getCanImportShoppingCallMaterial");//获取可以导入到商城的物料
		
		
		
		
		
		
		//免登陆接口
		canNotLoginUrlSet.add("getAllShoppingCallMaterialData");
		canNotLoginUrlSet.add("getShoppingCallMaterialType");
		canNotLoginUrlSet.add("getShoppingCallCompanyByIds");
		
		
		
//		urlMethodMappingMap.put("relationSuppilerAndCompany", "relationSuppilerAndCompany");//关联供应商和公司  取消
//		urlMethodMappingMap.put("importShoppingCallCompanyToSuppiler", "importShoppingCallCompanyToSuppiler");//将商城公司导入为供应商 取消
//		urlMethodMappingMap.put("importMaterialToCompany", "importMaterialToCompany");//将物料导入到公司  取消
//		urlMethodMappingMap.put("collectionShoppingMaterial", "collectionShoppingMaterial");//收藏物料 取消
//		urlMethodMappingMap.put("getCollectionMaterialData", "getCollectionMaterialData");//获取收藏夹里的物料信息
//		urlMethodMappingMap.put("removeCollection", "removeCollection");//移除收藏夹中的信息
//		urlMethodMappingMap.put("checkImportMaterialIsRepeat", "checkImportMaterialIsRepeat");//校验物料是否存在在本公司  取消
        
		
		
	}
	
}
